package com.ship.boats.business.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.ship.common.core.constant.ShipApiConstants;
import com.ship.common.core.utils.OkHttpUtil;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import org.apache.commons.collections4.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Properties;

/**
 * <b>BoatsSystemServiceImpl</b>
 *
 * @description: BoatsSystemServiceImpl <br>
 * @date: 2024/3/26 11:41 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class BoatsSystemServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(BoatsSystemServiceImpl.class);

    @Value("${boats.url}")
    private String boatsUrl;

    public Object getBoatsUtil(Map<String,String> bodyMap, String url){
        com.alibaba.fastjson.JSONObject paramJson = new com.alibaba.fastjson.JSONObject();
        bodyMap.forEach((k,v) -> {
            paramJson.put(k,v);
        });
        RequestBody body = RequestBody.create(paramJson.toJSONString(), MediaType.parse("application/json"));
        Map<String, String> headers = new HashedMap<>();
        headers.put("appKey", ShipApiConstants.TREASURE_BOAT_APPKEY);
        Object resultObj = OkHttpUtil.sendOkHttpClient(boatsUrl+ url
                , "POST",body,headers);
        Map<String,Object> map = (Map) com.alibaba.fastjson.JSONObject.toJSON(JSONUtil.parse(resultObj));
        if (CollectionUtil.isNotEmpty(map)){
            if ("0".equals(map.get("code"))){
                return map.get("data");
            }
        }
        return null;
    }

    public Object getSisterImoUtil(Map<String, String> bodyMap, String querySisterimoUrl) {
        com.alibaba.fastjson.JSONObject paramJson = new com.alibaba.fastjson.JSONObject();
        bodyMap.forEach((k,v) -> {
            paramJson.put(k,v);
        });
        RequestBody body = RequestBody.create(paramJson.toJSONString(), MediaType.parse("application/json"));
        Map<String, String> headers = new HashedMap<>();
        headers.put("appKey", ShipApiConstants.TREASURE_BOAT_APPKEY);
        Object resultObj = OkHttpUtil.sendOkHttpClient(querySisterimoUrl
                , "POST",body,headers);
        Map<String,Object> map = (Map) com.alibaba.fastjson.JSONObject.toJSON(JSONUtil.parse(resultObj));
        if (CollectionUtil.isNotEmpty(map)){
            if ("0".equals(map.get("code"))){
                return map.get("data");
            }
        }
        return null;
    }
}
